
#include "stdafx.h"
#include <QFileInfo>
#include <QTextStream>
#include "pf_fileio.h"

PF_FileIO* PF_FileIO::uniqueInstance = NULL;

/**
 * Calls the import method of the filter responsible for the format
 * of the given file.
 *
 * @param graphic The container to which we will add
 *        entities. Usually that's an PF_Graphic entity but
 *        it can also be a polyline, text, ...
 * @param file Path and name of the file to import.
 */
bool PF_FileIO::fileImport(PF_Graphic& graphic, const QString& file,
		LX_Define::FormatType type) {

    PF_DEBUG->print("Trying to import file '%s'...", file.toLatin1().data());

    PF_FilterInterface* filter = NULL;

	LX_Define::FormatType t;
	if (type == LX_Define::FormatUnknown) {
		t = detectFormat(file);
	}
	else {
		t = type;
	}

	filter = getImportFilter(t);

	

    if (filter!=NULL) {
        return filter->fileImport(graphic, file, t);
    }
	else {
		PF_DEBUG->print(PF_Debug::D_WARNING,
			"PF_FileIO::fileImport: failed to import file: %s", 
                        file.toLatin1().data());
	}
	
	return false;
}



/**
 * Calls the export method of the object responsible for the format
 * of the given file.
 *
 * @param file Path and name of the file to import.
 */
bool PF_FileIO::fileExport(PF_Graphic& graphic, const QString& file,
		LX_Define::FormatType type) {

    PF_DEBUG->print("PF_FileIO::fileExport");
    //PF_DEBUG->print("Trying to export file '%s'...", file.toLatin1());

	if (type==LX_Define::FormatUnknown) {
        QString extension;
        extension = QFileInfo(file).suffix().toLower();

		if (extension=="dxf") {
			type = LX_Define::FormatDXF;
		}
		else if (extension=="font") {
			type = LX_Define::FormatFONT;
		}
	}

	PF_FilterInterface* filter = getExportFilter(type);
	if (filter!=NULL) {
		return filter->fileExport(graphic, file, type);
	}
	
    PF_DEBUG->print("PF_FileIO::fileExport: no filter found");

	return false;
}


/**
 * Detects and returns the file format of the given file.
 */
LX_Define::FormatType PF_FileIO::detectFormat(const QString& file) {
    LX_Define::FormatType type = LX_Define::FormatUnknown;
    QFileInfo fi(file);

    QString ext = fi.suffix().toLower();
    if (ext=="font") {
        type = LX_Define::FormatFONT;
    } else if (ext=="dxf") {
        type = LX_Define::FormatDXF1;
        QFile f(file);

        if (!f.open(QIODevice::ReadOnly)) {
            // Error opening file:
            PF_DEBUG->print(PF_Debug::D_WARNING,
                                "PF_FileIO::detectFormat: Cannot open file: %s", file.toLatin1().data());
            type = LX_Define::FormatUnknown;
        } else {
            PF_DEBUG->print("PF_FileIO::detectFormat: "
				"Successfully opened DXF file: %s",
                file.toLatin1().data());

            QTextStream ts(&f);
            QString line;
            int c=0;
            while (!ts.atEnd() && ++c<100) {
                line = ts.readLine();
                if (line=="$ACADVER") {
                    type = LX_Define::FormatDXF;
                }
				// very simple reduced DXF:
                if (line=="ENTITIES" && c<10) {
					type = LX_Define::FormatDXF;
				}
            }
            f.close();
        }
    }

    return type;
}


// EOF
